package com.broad.commerce.code.services.jdbc;

import com.broad.commerce.code.base.AbstractJavaService;
import com.broad.commerce.code.modules.BaseModule;
import com.broad.commerce.code.modules.jdbc.JpaModule;
import com.broad.commerce.web.exception.ServiceException;
import org.springframework.stereotype.Service;

/**
 * @author stephen
 * @version V1.0
 * @date 2022-02-07 15:07
 * @description: JpaModuleService
 **/
@Service
public class JpaModuleService extends AbstractJavaService {
	/**
	 * Create entity code string.
	 * @param baseModule the base module
	 * @return the string
	 * package com.broad.commerce.jdbc.repository.system.jpa;
	 * <br/>
	 * import com.broad.commerce.jdbc.entity.system.SystemUser;
	 * import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
	 * import org.springframework.data.repository.PagingAndSortingRepository;
	 * <br/>
	 * * @author stephen
	 * * @version V1.0
	 * * @date 2020-08-26 17:33
	 * * @description: UserDao
	 * public interface SystemUserDao extends PagingAndSortingRepository<SystemUser, Long>, JpaSpecificationExecutor<SystemUser> {
	 * SystemUser findByUsername(String userName);
	 * SystemUser findByAccount(String account);
	 * SystemUser findByPhone(String phone);
	 * }
	 **/
	@Override
	public String createClassCode(BaseModule baseModule) {
		if (!(baseModule instanceof JpaModule)) {
			throw new ServiceException("传入参数类型错误！应为 JpaModule");
		}
		return super.createClassCode(baseModule);
	}

	/**
	 * Create import code.
	 * @param sb the sb
	 * @param baseModule the base module
	 */
	@Override
	public void createImportCode(StringBuilder sb, BaseModule baseModule) {
		JpaModule jpaModule = (JpaModule) baseModule;
		//@off
		sb.append("import ").append(jpaModule.getEntityFullClassName()).append(";\n")
		  .append("import org.springframework.data.jpa.repository.JpaRepository;").append("\n")
		  .append("import org.springframework.data.jpa.repository.JpaSpecificationExecutor;").append("\n")
		  .append("import org.springframework.stereotype.Repository;").append("\n")
		  .append("\n");//@on
	}

	/**
	 * Create annotation code.
	 * @param sb the sb
	 * @param baseModule the base module
	 */
	@Override
	public void createAnnotationCode(StringBuilder sb, BaseModule baseModule) {
		sb.append("@Repository").append("\n");
	}

	/**
	 * Create class start tag.
	 * @param sb the sb
	 * @param baseModule the base module
	 */
	@Override
	protected void createClassStartTag(StringBuilder sb, BaseModule baseModule) {
		JpaModule jpaModule = (JpaModule) baseModule;
		//@off
		sb.append("public interface ").append(jpaModule.getClassName())
		  .append(" extends JpaRepository<").append(jpaModule.getEntityClassName())
		  .append(", Long>, JpaSpecificationExecutor<").append(jpaModule.getEntityClassName()).append("> {\n")
		  .append("\n");//@on
	}

	/**
	 * Create class content.
	 * @param sb the sb
	 * @param baseModule the base module
	 */
	@Override
	public void createClassContent(StringBuilder sb, BaseModule baseModule) {

	}
}
